package com.vmj.instructions.references;

import com.vmj.heap.VMClass;
import com.vmj.heap.ClassRef;
import com.vmj.heap.RuntimeConstantPool;
import com.vmj.heap.VMObject;
import com.vmj.instructions.base.Index16Instruction;
import com.vmj.rtda.Frame;
import com.vmj.rtda.OperandStack;

/**
 * Created by yangshoujun on 2017/9/16 下午8:23.
 */
public class InstanceOf extends Index16Instruction {
    @Override
    public void execute(Frame frame) {
        OperandStack stack = frame.getOperandStack();
        VMObject ref = stack.popRef();
        if(ref == null) {
            stack.pushBoolean(false);
            return;
        }

        RuntimeConstantPool cp = frame.getMethod().getClazz().getRuntimeConstantPool();
        ClassRef classRef = (ClassRef) cp.getConstant(getIndex()).getValue();
        VMClass clazz = classRef.resolvedClass();
        if(ref.isInstanceOf(clazz)) {
            stack.pushBoolean(true);
        } else {
            stack.pushBoolean(false);
        }
    }
}
